package com.gmall.product.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gmall.common.result.PageInfo;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseAttrInfo;
import com.gmall.model.product.BaseAttrValue;
import com.gmall.product.mapper.BaseAttrInfoMapper;
import com.gmall.product.mapper.BaseAttrValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Order(1)
public class BaseAttrService {

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;


    public Result<PageInfo<BaseAttrInfo>> attrInfoList(
            Integer pageNo,Integer pageSize,
            Long category1Id, Long category2Id, Long category3Id) {
        //mp 只能单表操作

        //1. 查询平台属性
        /**
          SELECT bai.id,bai.category_id,bai.attr_name,bai.category_level,
          bai.create_time,bai.update_time,bai.is_deleted
           from base_attr_info bai where (
              (bai.category_id = 2 and bai.category_level = 1)
              or (bai.category_id = 30 and bai.category_level = 2)
              or( bai.category_id = 192 and bai.category_level = 3)
          )
          order by bai.id
         */
        QueryWrapper<BaseAttrInfo> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BaseAttrInfo> lambda = queryWrapper.lambda();

        if(category1Id!=null && category1Id!=0){
            lambda.or(a ->{
                a.eq(BaseAttrInfo::getCategoryId,category1Id)
                 .eq(BaseAttrInfo::getCategoryLevel,1);
            });
        }
        if(category2Id!=null && category2Id!=0){
            lambda.or(a ->{
                a.eq(BaseAttrInfo::getCategoryId,category2Id)
                        .eq(BaseAttrInfo::getCategoryLevel,2);
            });
        }
        if(category3Id!=null && category3Id!=0){
            lambda.or(a ->{
                a.eq(BaseAttrInfo::getCategoryId,category3Id)
                        .eq(BaseAttrInfo::getCategoryLevel,3);
            });
        }
        lambda.orderByAsc(BaseAttrInfo::getId);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<BaseAttrInfo> page =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo,pageSize);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<BaseAttrInfo> baseAttrInfoPage = baseAttrInfoMapper.selectPage(page,
                queryWrapper);



        PageInfo<BaseAttrInfo> pageInfo = new PageInfo<>();
        pageInfo.setTotal(baseAttrInfoPage.getTotal());
        pageInfo.setRecords(baseAttrInfoPage.getRecords());
        //对于每一个属性，查询对应的属性值
        //  SELECT * from base_attr_value where attr_id =23
        baseAttrInfoPage.getRecords().forEach(baseAttrInfo -> {

            QueryWrapper<BaseAttrValue> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(queryWrapper1);
            baseAttrInfo.setAttrValueList(baseAttrValues);//属性值
        });
        return Result.ok(pageInfo);
    }

    public Result<PageInfo<BaseAttrInfo>> attrInfoList1(Integer pageNo,Integer pageSize,Long category1Id, Long category2Id, Long category3Id) {
        PageHelper.startPage(pageNo,pageSize);
        Page<BaseAttrInfo> baseAttrInfos = baseAttrInfoMapper.attrInfoList1(category1Id, category2Id, category3Id);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotal(baseAttrInfos.getTotal());
        pageInfo.setRecords(baseAttrInfos);
        return Result.ok(pageInfo);
    }
    public Result<PageInfo<BaseAttrInfo>> attrInfoList2(Integer pageNo,Integer pageSize,Long category1Id, Long category2Id, Long category3Id) {
        PageHelper.startPage(pageNo,pageSize);
        Page<BaseAttrInfo> baseAttrInfos = baseAttrInfoMapper.attrInfoList2(category1Id, category2Id, category3Id);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotal(baseAttrInfos.getTotal());
        pageInfo.setRecords(baseAttrInfos);
        return Result.ok(pageInfo);
    }


    @Transactional
    public Result<String> save(BaseAttrInfo baseAttrInfo) {

        if(baseAttrInfo.getId()==null){//新增
            baseAttrInfoMapper.insert(baseAttrInfo);

            Long attrId = baseAttrInfo.getId();
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();

            attrValueList.forEach((attrValue)->{
                attrValue.setAttrId(attrId);
                baseAttrValueMapper.insert(attrValue);
            });

            return Result.ok("保存成功");
        }else{
            baseAttrInfoMapper.updateById(baseAttrInfo);//修改主表

            Long attrId = baseAttrInfo.getId();
            //delete from base_attr_value where attr_id = ?
            UpdateWrapper<BaseAttrValue> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(BaseAttrValue::getAttrId,attrId);
            baseAttrValueMapper.delete(updateWrapper);

            //重新保存
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();

            attrValueList.forEach((attrValue)->{
                attrValue.setAttrId(attrId);
                baseAttrValueMapper.insert(attrValue);
            });
            return Result.ok("修改成功");
        }
    }

    @Transactional
    public Result<String> remove(Long attrId) {

        //删除属性，同时删除属性值
        //delete from base_attr_value where attr_id = ?
        UpdateWrapper<BaseAttrValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BaseAttrValue::getAttrId,attrId);
        baseAttrValueMapper.delete(updateWrapper);

        //删除主表
        baseAttrInfoMapper.deleteById(attrId);
        return Result.ok("删除成功");
    }
}
